package com.xd.lib.dialog;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.xd.lib.Const;
import com.xd.lib.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/6/12.
 */
public class DialogActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_layout);

        int[] icons = {
                R.drawable.dialog1
        };
        String[][] funcNames = {
                {"自定义对话框1", ""}
        };

        List<Map<String, Object>> data = new ArrayList<>();
        for (int i=0;i<funcNames.length;i++){
            Map<String, Object> map = new HashMap<>();
            map.put(Const.STRING_ICON, icons[i]);
            map.put(Const.STRING_TITLE, funcNames[i][Const.INDEX_LISTVIEW_TITLE]);
            map.put(Const.STRING_DESCRIPTION, funcNames[i][Const.INDEX_LISTVIEW_DESCRIPTION]);
            data.add(map);
        }

        SimpleAdapter adapter = new SimpleAdapter(
                this,
                data,
                R.layout.listview_item,
                new String[]{Const.STRING_ICON, Const.STRING_TITLE, Const.STRING_DESCRIPTION},
                new int[]{R.id.listview_item_image, R.id.listview_item_txt_title, R.id.listview_item_txt_description}
        );

        final ListView listView = (ListView)findViewById(R.id.listview);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int index = listView.getCount() - position;
                Intent intent = null;
                switch (index){
                    case 1:
                        DialogUtil.showDialog1(DialogActivity.this,
                                "确认花掉10个金币购买该物品吗？",
                                new IDialogButtonClickListener() {
                                    @Override
                                    public void onClick() {
                                        Toast.makeText(DialogActivity.this, "花费10个金币", Toast.LENGTH_SHORT).show();
                                    }
                                });
                        break;
                }
                if (intent != null) {
                    DialogActivity.this.startActivity(intent);
                }
            }
        });
    }
}
